package com.example.zyfypt516wh.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.apdater.TcaseAdapter;
import com.example.zyfypt516wh.bean.ResBean;
import com.example.zyfypt516wh.iface.ResListener;
import com.example.zyfypt516wh.model.ResModel;

import java.util.List;

public class Fragment42 extends BaseFragments{
    private List<ResBean> list;
    private RecyclerView recyclerView;
    private TcaseAdapter adapter;

    private LinearLayoutManager layoutManager;
    private int page=1;
    private int lastVisibleItemPosition;

    private ResListener listener=new ResListener() {
        @Override
        public void onResponse(List<ResBean> beanList) {
            if(page==1)
            {
                list=beanList;
            }else {
                list.remove(beanList);
                list.addAll(beanList);
            }
            //list中就是最新数据
            adapter.setList(list);//刷新列表
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "失败"+msg, Toast.LENGTH_SHORT).show();
            System.out.println("--失败"+msg);
        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        //动态加载frament布局
        return inflater.inflate(R.layout.fragment42,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        System.out.println("--fragment42--"+getSessionID());

        initRV(view);
        ResModel model=new ResModel();
        model.getResultList("project",page,getSessionID(),listener);
    }

    private void initRV(View view) {
        recyclerView=view.findViewById(R.id.rv42);

        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);

        adapter=new TcaseAdapter(context);
        recyclerView.setAdapter(adapter);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(list!=null)
                {
                    if(newState==RecyclerView.SCROLL_STATE_IDLE&&
                            lastVisibleItemPosition+1==list.size()){
                        page+=1;
                        ResModel model=new ResModel();
                        model.getResultList("project",page,getSessionID(),listener);
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=layoutManager.findLastVisibleItemPosition();
            }
        });
    }
}